package com.rd.controller;


import com.rd.pojo.Posts;
import com.rd.service.IPostsService;
import com.rd.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lsl
 * @since 2024-06-06
 */
@RestController
@RequestMapping("/api")
public class PostsController {

    @Autowired
    private IPostsService postsRepository;

    @PostMapping("/submit_post")
    public R submitPost(@RequestBody Posts posts) {
        if (posts.getTitle() != null && posts.getContent() != null) {
            postsRepository.save(posts);
            return R.ok().setMsg("发送成功!");
        } else {
            return R.fail("标题或内容不能为空！");
        }
    }
    @GetMapping("/posts")
    public R getAllPosts() {
        return R.ok().setData(postsRepository.list());
    }
}
